package com.steven.ourcontest.app

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import com.steven.ourcontest.chat.view.ChatActivity
import com.steven.ourcontest.chat.view.ChatFriendsHistoryActivity
import com.steven.ourcontest.chat.view.FriendsActivity
import com.steven.ourcontest.chat.view.NewFriendsActivity
import com.steven.ourcontest.chat.view.UserDetailActivity
import com.steven.ourcontest.collect.view.CollectionActivity
import com.steven.ourcontest.contest.view.AddMatchActivity
import com.steven.ourcontest.contest.view.MatchDetailActivity
import com.steven.ourcontest.contest.view.SearchMatchActivity
import com.steven.ourcontest.forum.view.ForumDiscussActivity
import com.steven.ourcontest.forum.view.WriteForumActivity
import com.steven.ourcontest.information.view.SelectIdentityActivity
import com.steven.ourcontest.information.view.SelectSchoolActivity
import com.steven.ourcontest.information.view.UploadImageActivity
import com.steven.ourcontest.information.view.admin.AdminManageActivity
import com.steven.ourcontest.information.view.student.ConfirmStudentInfoActivity
import com.steven.ourcontest.information.view.teacher.ConfirmTeacherInfoActivity
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.login.view.LoginActivity
import com.steven.ourcontest.main.view.MainActivity
import com.steven.ourcontest.report.view.HandleReportActivity
import com.steven.ourcontest.report.view.ReportActivity
import com.steven.ourcontest.setting.view.SettingActivity
import com.steven.ourcontest.team.view.AddTeamActivity
import com.steven.ourcontest.team.view.TeamApplicationActivity
import com.steven.ourcontest.team.view.TeamDetailActivity
import com.steven.ourcontest.utils.Identity

object AppRouter {
    fun gotoMainActivity(context: Context?, action: String) {
        if (context == null)
            return
        val intent = Intent(context, MainActivity::class.java)
        intent.putExtra("action", action)
        context.startActivity(intent)
    }

    fun gotoLoginActivity(activity: Activity, action: String) {
        val intent = Intent(activity, LoginActivity::class.java)
        intent.putExtra("action", action)
        activity.startActivityForResult(intent, 100)
    }

    fun startIdentifyActivity(context: Context?, identity: Identity) {
        val intent = Intent(context, SelectSchoolActivity::class.java)
        intent.putExtra("identity", identity)
        context?.startActivity(intent)
    }

    fun gotoConfirmStudentInfoActivity(context: Context?, school: String, identity: Identity) {
        when (identity) {
            Identity.STUDENT -> {
                val intent = Intent(context, ConfirmStudentInfoActivity::class.java)
                intent.putExtra("school", school)
                intent.putExtra("identity", Identity.STUDENT)
                context?.startActivity(intent)
            }
            Identity.TEACHER -> {
                val intent = Intent(context, ConfirmTeacherInfoActivity::class.java)
                intent.putExtra("school", school)
                intent.putExtra("identity", Identity.TEACHER)
                context?.startActivity(intent)
            }
            else -> {
            }
        }
    }

    fun startUploadStudentImageActivity(context: Context, identity: Identity) {
        val intent = Intent(context, UploadImageActivity::class.java)
        intent.putExtra("identity", identity)
        context.startActivity(intent)
    }

    fun startSelectIdentityActivity(context: Context) {
        val intent = Intent(context, SelectIdentityActivity::class.java)
        context.startActivity(intent)
    }

    fun startFormDiscussActivity(context: Context, fid: Long) {
        val intent = Intent(context, ForumDiscussActivity::class.java)
        intent.putExtra("fid", fid)
        context.startActivity(intent)
    }

    fun startWriteForumActivity(context: Context) {
        val intent = Intent(context, WriteForumActivity::class.java)
        context.startActivity(intent)
    }

    fun startSettingActivity(context: Context) {
        val intent = Intent(context, SettingActivity::class.java)
        context.startActivity(intent)
    }

    fun startAdminManageActivity(context: Context) {
        val intent = Intent(context, AdminManageActivity::class.java)
        context.startActivity(intent)
    }

    fun startFriendsActivity(context: Context) {
        val intent = Intent(context, FriendsActivity::class.java)
        context.startActivity(intent)
    }

    fun openUserDetailActivity(context: Context, uid: Long) {
        val intent = Intent(context, UserDetailActivity::class.java)
        if (LoginController.userId == uid)
            return
        intent.putExtra("uid", uid)
        context.startActivity(intent)
    }

    fun startChatActivity(context: Context, friendId: Long, name: String) {
        val intent = Intent(context, ChatActivity::class.java)
        intent.putExtra("friendId", friendId)
        intent.putExtra("name", name)
        context.startActivity(intent)
    }

    fun openChatFriendsHistoryActivity(context: Context) {
        val intent = Intent(context, ChatFriendsHistoryActivity::class.java)
        context.startActivity(intent)
    }

    fun startNewFriendsActivity(context: Context) {
        val intent = Intent(context, NewFriendsActivity::class.java)
        context.startActivity(intent)
    }

    fun openTeamDetailActivity(context: Context, teamId: Long) {
        val intent = Intent(context, TeamDetailActivity::class.java)
        intent.putExtra("teamId", teamId)
        context.startActivity(intent)
    }

    fun startAddTeamActivity(context: Context) {
        val intent = Intent(context, AddTeamActivity::class.java)
        context.startActivity(intent)
    }

    fun openTeamApplicationActivity(context: Context) {
        val intent = Intent(context, TeamApplicationActivity::class.java)
        context.startActivity(intent)
    }

    fun openCollectionActivity(context: Context) {
        val intent = Intent(context, CollectionActivity::class.java)
        context.startActivity(intent)
    }

    fun openReportActivity(context: Context, uid: Long) {
        val intent = Intent(context, ReportActivity::class.java)
        intent.putExtra("uid", uid)
        context.startActivity(intent)
    }

    fun openHandleReportActivity(context: Context) {
        val intent = Intent(context, HandleReportActivity::class.java)
        context.startActivity(intent)
    }

    fun openMatchDetailActivity(context: Context, matchId: Long) {
        val intent = Intent(context, MatchDetailActivity::class.java)
        intent.putExtra("matchId", matchId)
        context.startActivity(intent)
    }

    fun openWeb(context: Context, url: String) {
        val webHasHeadRegex =
            Regex("^(http|https|ftp)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\\$#\\=~_\\-@]*)*\$")
        if (url.matches(webHasHeadRegex)) {
            val intent = Intent()
            intent.data = Uri.parse(url)
            intent.action = Intent.ACTION_VIEW
            context.startActivity(Intent.createChooser(intent, "打开网址$url"))
        } else{
            Toast.makeText(context, "网址不正确", Toast.LENGTH_SHORT).show()
        }
    }

    fun openSearchMatchActivity(context: Context) {
        val intent = Intent(context, SearchMatchActivity::class.java)
        context.startActivity(intent)
    }

    fun openAddMatchActivity(context: Context) {
        val intent = Intent(context, AddMatchActivity::class.java)
        context.startActivity(intent)
    }
}